﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WITM.DAL;
using WITM.BLL;

namespace WITMTestApplication
{
    public partial class CategoriesTestForm : Form
    {
        private UserBL userBl;
        private SpendingCategoryBL spendingCategoryBl;

        public CategoriesTestForm()
        {
            InitializeComponent();
            InitializeDB(false);
            InitializeCategoriesGrid();
            LoadUsersComboBox();
        }

        private void InitializeCategoriesGrid()
        {
            dgvCategories.Columns.Add("colCategoryID", "ID");
            dgvCategories.Columns.Add("colDescription", "Description");
        }

        private void InitializeDB(bool tracking)
        {
            WhereIsTheMoneyDBEntities context = new WhereIsTheMoneyDBEntities();
            if (!tracking)
            {
                context.Users.MergeOption = System.Data.Objects.MergeOption.NoTracking;
                context.SpendingCategories.MergeOption = System.Data.Objects.MergeOption.NoTracking;
            }
            UnitOfWork unitOfWork = new UnitOfWork(context);
            userBl = new UserBL(unitOfWork);
            spendingCategoryBl = new SpendingCategoryBL(unitOfWork);
        }

        private void LoadUsersComboBox()
        {
            IEnumerable<Users> users = userBl.GetAll();
            var usersFormated = from u in users
                                select new
                                {
                                    ID = u.UserID,
                                    Name = String.Format("{0} {1}", u.FirstName, u.LastName)
                                };
            cbUsers.ValueMember = "ID";
            cbUsers.DisplayMember = "Name";
            cbUsers.DataSource = usersFormated.ToList();
        }

        private void LoadUserCategories()
        {
            int userID = (int)cbUsers.SelectedValue;
            IEnumerable<SpendingCategories> categories = spendingCategoryBl.GetAllPerUser(userID);
            dgvCategories.Rows.Clear();
            foreach (SpendingCategories category in categories)
            {
                dgvCategories.Rows.Add(category.CategoryID, category.Description);
            }
        }

        private void cbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadUserCategories();
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            InitializeDB(true);
            int userID = (int)cbUsers.SelectedValue;
            Users user = userBl.GetUserByID(userID);
            SpendingCategories category = new SpendingCategories()
            {
                Description = txtCategoryDesc.Text,
                Users = user
            };
            spendingCategoryBl.Add(category);
            txtCategoryDesc.Text = String.Empty;
            LoadUserCategories();
        }
    }
}
